﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using Common;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class LoginCtrlViewModel : BaseViewModel<LoginCtrlViewModel>
    {
        #region Variables

        private AccountModel _account = new AccountModel();

        private bool _login_status;

        #endregion

        #region Constructor
        public LoginCtrlViewModel()
        {

        }
        #endregion

        #region Properties

        public AccountModel CurrentAccount
        {
            get
            {
                return DataManager.Instance.Login;
            }

            set
            {
                //_account = value;
                DataManager.Instance.Login = value;
                RaisePropertyChanged("CurrentAccount");
            }
        }

        public bool Login_status
        {
            get
            {
                return _login_status;
            }

            set
            {
                _login_status = value;
            }
        }


        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void UpdateIPs()
        {
            IPAddress[] ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            string ips = "";
            foreach (IPAddress item in ip)
            {
                if (IsValidIP(item.ToString()))
                {
                    ips += item.ToString() + "|";
                }
            }
            DataManager.Instance.CurRoom.Ips = ips;
        }
        private bool IsValidIP(string ip)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
            {
                string[] ips = ip.Split('.');
                if (ips.Length == 4 || ips.Length == 6)
                {
                    if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                        return true;
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
            else
                return false;
        }

        private void Update()
        {
            UpdateIPs();
            DataManager.Instance.CurRoom.LoginStatus = "1";
            DataManager.Instance.CurRoom.AccountLinkId = DataManager.Instance.Login.Id;
            DataManager.Instance.CurRoom.LocalVersion = ClientAndServerHelper.Instance.LocalXMLContent.LocalVersion;
            if (string.IsNullOrEmpty(DataManager.Instance.CurRoom.Code))
            {
                DataManager.Instance.CurRoom.Code = "room_"+DataManager.Instance.CurRoom.Name;
            }
            ConfigOperationRoomModelService.Instance.Update(DataManager.Instance.CurRoom);
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        #endregion

        #region Command

        public void LoginExecute()
        {
            IEnumerable<AccountModel> ounts = AccountService.Instance.seachAccountByNameAndPwd(DataManager.Instance.Login.LoginName, DataManager.Instance.Login.Pwd.MD5Encrypt32());
            if (ounts.Count() >0)
            {
                Update();
                Login_status = true;
                DataManager.Instance.Login = (AccountModel)ounts.ToList()[0];
                ConfigOperationRoomModelService.Instance.Update(DataManager.Instance.CurRoom);
            }
            else
            {
                Login_status = false;
            }
        }
        #endregion
    }
}
